﻿#include "operatewidget.h"
#include <QHBoxLayout>
#include "ui_operatewidget.h"
#include "funclistitem.h"
#include "utils.h"
#include <QMessageBox>

OperateWidget::OperateWidget(QWidget *parent)
    : QWidget{parent}
    ,ui(new Ui::OperateWidget)
{
    ui->setupUi(this);

    this->setWindowIcon(QIcon(":/img/tinyDisk.png"));
    this->setWindowTitle("TinyDisk | "+m_usrName);
    m_pFriend = new Friend;
    m_filewidget = new FileWidget;
    ui->sw_Page->addWidget(m_pFriend);
    ui->sw_Page->addWidget(m_filewidget);

    FuncListItem *m_pListItemFriend = new FuncListItem(":/img/Friend.png","好友",ui->lw_FuncMenu);
    FuncListItem *m_pListItemBook = new FuncListItem(":/img/Book.png","文件",ui->lw_FuncMenu);

    QListWidgetItem *item1 = new QListWidgetItem(ui->lw_FuncMenu);
    item1->setSizeHint(QSize(80,80));
    ui->lw_FuncMenu->setItemWidget(item1, m_pListItemFriend);
    QListWidgetItem *item2 = new QListWidgetItem(ui->lw_FuncMenu);
    item2->setSizeHint(QSize(80,80));
    ui->lw_FuncMenu->setItemWidget(item2, m_pListItemBook);
    connect(ui->lw_FuncMenu,SIGNAL(currentRowChanged(int)),
            ui->sw_Page,SLOT(setCurrentIndex(int)));

    ui->lw_FuncMenu->setCurrentRow(1);

    ui->widget->setStyleSheet("background-color: white;");


    // 设置 QPushButton 的样式
    ui->pushButton->setStyleSheet(
        "QPushButton{"
        "border-radius: 5px;"
        "border: 1px solid #d0d0d0;"          // 边框为灰色
        "padding: 4px;"                    // 设置内边距
        "}"

        // 设置按钮被点击或聚焦时的样式
        "QPushButton:hover {"
        "border: 1px solid rgb(0,107,190);"          // 边框为灰色
        "background-color: rgb(224,238,249);"     // 修改背景颜色为黑色
        "color: black;"                            // 修改文本颜色为白色
        "}"

        // 设置按钮被按下时的样式
        "QPushButton:pressed {"
        "border: 1px solid rgb(0,84,154);"          // 边框为灰色
        "background-color: rgb(204,228,247);"     // 修改背景颜色为黑色
        "color: black;"                           // 修改文本颜色为白色
        "}"
        );

    // 设置 QLineEdit 的背景颜色为浅灰色，并设置圆角
    ui->lineEdit->setStyleSheet(
        "QLineEdit{"
        "background-color: #f0f0f0;"
        " border-radius: 10px;"
        " padding: 2px;"
        "}"
        "QLineEdit:focus{"
        "border: 1px solid rgb(0,84,154);"
        "background-color: white;"
        "}"

        );
    utils::setRoundRectImg(ui->lb_avatar,":/img/avatar.png",10);

}

OperateWidget::~OperateWidget()
{
}

OperateWidget &OperateWidget::getInstance()
{
    static OperateWidget instance;
    return instance;
}

Friend *OperateWidget::getFriend()
{
    return m_pFriend;
}

FileWidget *OperateWidget::getFileWidget()
{
    return m_filewidget;
}

void OperateWidget::setUsrName(QString usrName)
{
    m_usrName = usrName;
    this->setWindowTitle("TinyDisk | "+m_usrName);
}

void OperateWidget::handleAddFriendRequst(PDU *pdu)
{
    char caName[32] = {'\0'};
    strncpy_s(caName,pdu->caData,32);
    int res = QMessageBox::information(this,"添加好友",QString("%1 wang to add you as friend").arg(caName),QMessageBox::Yes,QMessageBox::No);
    PDU *respdu = mkPDU(0);
    memcpy(respdu->caData,pdu->caData,64);
    if(res == QMessageBox::Yes)
    {
        respdu->uiMsgType = ENUM_MSG_TYPE_ADD_FRIEND_AGREE;
    }
    else
    {
        respdu->uiMsgType = ENUM_MSG_TYPE_ADD_FRIEND_REJECT;
    }
    ClientDiskTcpSocket::getInstance()->write((char*)respdu,respdu->uiPDULen);
    free(respdu);
    respdu = NULL;
}

void OperateWidget::handleAddFriendAgeree(PDU *pdu)
{
    char caPerName[32] = {'\0'};
    memcpy(caPerName, pdu->caData+32, 32);
    QMessageBox::information(this, "添加好友", QString("添加%1好友成功").arg(caPerName));

}

void OperateWidget::handleAddFriendRefuse(PDU *pdu)
{
    char caPerName[32] = {'\0'};
    memcpy(caPerName, pdu->caData +32, 32);
    QMessageBox::information(this, "添加好友", QString("添加%1好友失败").arg(caPerName));
}
